<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-7-27
 * Time: 下午3:19
 */
namespace Tiny;

use DB\Token;

class ServiceRequestData{
  public $token;
}

class ServiceResponseData{
  public $code;
  public function __construct() {
    $this->code = HttpStatus::SUC;
  }
}

abstract class ServiceAPI extends JsonAPI{
  protected function beforeRun(Request $request, Response $response): bool {
    if (!parent::beforeRun($request,$response)) return false;
    /**
     * @var $rqData ServiceRequestData
     */
    $rqData = $request->data;
    if (!isset($rqData->token) || !is_string($rqData->token)){
      $response->httpStatus = HttpStatus::ARGS_ERROR;
      $response->httpStatusMsg = "token is not set or is not string";
      return false;
    }
    $token = new Token();
    if(!$token->isValid($rqData->token)){
      $response->httpStatus = HttpStatus::ARGS_ERROR;
      $response->httpStatusMsg = "token is not set or is expired";
      return false;
    }
    $request->uid = $token->getUid();
    return true;
  }
}